home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from LocaleInfo import LocaleInfo
- import os.path as os
- import sys
- import subprocess
-
- class ImSwitch(object):
- global_confdir = '/etc/X11/xinit/xinput.d/'
- local_confdir = os.path.expanduser('~/.xinput.d/')
- bin = '/usr/bin/im-switch'
- default_method = 'scim-bridge'
-
- def __init__(self):
- pass
-
-
- def available(self):
- ''' return True if im-switch is available at all '''
- return os.path.exists(self.bin)
-
-
- def enabledForLocale(self, locale):
- ''' check if we have a config for this specifc locale (e.g. ja_JP) '''
- for dir in (self.local_confdir, self.global_confdir):
- for name in (locale, 'all_ALL'):
- target = os.path.join(dir, name)
- if os.path.exists(target):
- im_name = os.path.basename(os.path.realpath(target))
- if im_name in ('none', 'default'):
- return False
- return True
-
-
- return False
-
-
- def enable(self, locale):
- ''' enable input methods for locale'''
- subprocess.call([
- 'im-switch',
- '-z',
- locale,
- '-a'])
- if not self.enabledForLocale(locale):
- subprocess.call([
- 'im-switch',
- '-z',
- locale,
- '-s',
- self.default_method])
-
-
-
- def disable(self, locale):
- ''' disable input method for locale '''
- if os.path.exists(os.path.join(self.local_confdir, locale)):
- os.unlink(os.path.join(self.local_confdir, locale))
-
- if self.enabledForLocale(locale):
- subprocess.call([
- 'im-switch',
- '-z',
- locale,
- '-s',
- 'none'])
-
-
-
- def getInputMethodForLocale(self, locale):
- for dir in (self.local_confdir, self.global_confdir):
- for name in (locale, 'all_ALL'):
- target = os.path.join(dir, name)
- if os.path.exists(target):
- return os.path.basename(os.path.realpath(target))
-
-
-
-
- def getAvailableInputMethods(self):
- ''' get the input methods available via im-switch '''
- inputMethods = []
- for dentry in os.listdir(self.confdir):
- if not os.path.islink(self.confdir + dentry):
- inputMethods.append(dentry)
- continue
-
- inputMethods.sort()
- return inputMethods
-
-
- def setDefaultInputMethod(self, method, locale = 'all_ALL'):
- ''' sets the default input method for the given locale
- (in ll_CC form)
- '''
- l = self.confdir + locale
- if os.path.islink(l):
- os.unlink(l)
-
- os.symlink(self.confdir + method, l)
- return True
-
-
- def resetDefaultInputMethod(self, locale = 'all_ALL'):
- ''' reset the default input method to auto (controlled by
- im-switch
- '''
- d = '/etc/alternatives/xinput-%s' % locale
- l = self.confdir + locale
- if os.path.islink(l):
- os.unlink(l)
-
- os.symlink(d, self.confdir + locale)
- return True
-
-
- def getCurrentInputMethod(self, locale = 'all_ALL'):
- ''' get the current default input method for the selected
- locale (in ll_CC form)
- '''
- return os.path.basename(os.path.realpath(self.confdir + locale))
-
-
- if __name__ == '__main__':
- im = ImSwitch()
- print im.getInputMethodForLocale('ja_JP')
- print im.enabledForLocale('all_ALL')
- sys.exit(1)
- print 'available input methods: '
- print im.getAvailableInputMethods()
- print 'current method: ', im.getCurrentInputMethod()
- print "switching to 'th-xim': ", im.setDefaultInputMethod('th-xim')
- print 'current method: ', im.getCurrentInputMethod()
- print 'reset default: ', im.resetDefaultInputMethod()
- print 'current method: ', im.getCurrentInputMethod()
-
-